﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows;

namespace XMM.Objects
{
    public class PlaybackHistoryRecord : Object
    {
        /// <summary>
        /// History-Record Database-ID.
        /// </summary>
        public int RecordID { get; set; }

        /// <summary>
        /// Date of the playback.
        /// </summary>
        public DateTime PlaybackDate { get; set; }

        /// <summary>
        /// Database-ID of the watching audience.
        /// </summary>
        public int AudienceID { get; set; }

        /// <summary>
        /// Media-Type as integer.
        /// </summary>
        /// <remarks>0...unknown
        /// 1...Movie
        /// 2...Episode
        /// 3...Music
        /// 4...Picture</remarks>
        public int MediaType { get; set; }

        /// <summary>
        /// Database-ID of the played media-item.
        /// </summary>
        public int MediaID { get; set; }

        public string AudienceName
        {
            get
            {
                return resolveAudienceID();
            }
        }

        public string RecordString
        {
            get
            {
                return AudienceName + " (" + PlaybackDate.ToShortDateString() + ")";
            }
        }

        public string DescriptionString
        {
            get
            {
                string sDescriptionString = String.Empty;

                switch (MediaType)
                {
                    case 1:
                        Objects.Movie oMovieItem = new Movie(MediaID);
                        sDescriptionString = "Film: " + oMovieItem.Name + " (" + oMovieItem.Year.ToString() + ")";
                        break;
                    case 2:
                        Objects.SeriesEpisode oEpisodeItem = new SeriesEpisode(MediaID);
                        Objects.Series oSeries = new Series(oEpisodeItem.SeriesID);
                        Objects.SeriesSeason oSeason = new SeriesSeason(oEpisodeItem.SeasonID);
                        sDescriptionString = "Serie: " + oSeries.Name + " - Staffel " + oSeason.Number.ToString() + ", Folge " + oEpisodeItem.EpisodeNo.ToString() + " \"" + oEpisodeItem.Name + "\"";
                        break;
                    default:
                        break;

                }

                return sDescriptionString;
            }
        }

        /*
        public PlaybackHistoryRecord()
        {
            blankValues();
        }*/

        /// <summary>
        /// Generate PlaybackHistoryRecord-Object from Database-Record.
        /// </summary>
        /// <param name="iRecordID">(int) Target-Record Database-ID.</param>
        public PlaybackHistoryRecord(int iRecordID)
        {
            blankValues();

            Load(iRecordID);
        }

        public override string ToString()
        {
            switch (MediaType)
            {
                case 1:
                    Objects.Movie oMovie = new Movie(MediaID);
                    return "Film: " + oMovie.Name + " (" + oMovie.Year.ToString() + ")";
                    break;
                case 2:
                    Objects.SeriesEpisode oEpisode = new SeriesEpisode(MediaID);
                    Objects.Series oSeries = new Series(oEpisode.SeriesID);
                    Objects.SeriesSeason oSeason = new SeriesSeason(oEpisode.SeasonID);
                    return "Serie: " + oSeries.Name + " S" + oSeason.Number.ToString() + " E" + oEpisode.EpisodeNo.ToString() + " - \"" + oEpisode.Name + "\"";
                    break;
                default:
                    break;
            }

            return base.ToString();
        }

        /// <summary>
        /// Get the Name of the Audience of this Record.
        /// </summary>
        /// <returns></returns>
        public string resolveAudienceID()
        {
            Objects.Audition oAudience = new Audition(AudienceID);

            if (oAudience != null && oAudience.AuditionID != 0)
            {
                return oAudience.Name;
            }

            return "Unbekannt";
        }

        /// <summary>
        /// Reset all values.
        /// </summary>
        public void blankValues()
        {
            RecordID = 0;
            PlaybackDate = DateTime.MinValue;
            AudienceID = 0;
            MediaType = 0;
            MediaID = 0;
        }

        /// <summary>
        /// Load a specific record.
        /// </summary>
        /// <param name="iRecordID"></param>
        /// <returns></returns>
        public bool Load(int iRecordID)
        {
            if (iRecordID != 0)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_history vHistoryRecord = (from record in dbConnection.t_history where record.ID == iRecordID select record).Single();

                    if (vHistoryRecord != null && vHistoryRecord.ID != 0)
                    {
                        RecordID = vHistoryRecord.ID;
                        PlaybackDate = vHistoryRecord.PlaybackDate;
                        AudienceID = vHistoryRecord.AudienceID;
                        MediaType = vHistoryRecord.MediaType;
                        MediaID = vHistoryRecord.MediaID;
                        return true;
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Fehler beim Laden des Wiedergabe-History-Eintrags '" + iRecordID.ToString() + "': " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return false;
        }

        /// <summary>
        /// Create a new record and save it to database.
        /// </summary>
        /// <param name="iAudienceID">(int) Database-ID of the watching audience.</param>
        /// <param name="iMediaType">(int) Media-Type as integer.</param>
        /// <param name="iMediaID">(int) Media-Item Database-ID.</param>
        /// <returns>[true] if the record was created successfully.</returns>
        /// <remarks>PlaybackDate will be set to current Date.</remarks>
        public static bool CreateRecord(int iAudienceID, int iMediaType, int iMediaID)
        {
            if (iAudienceID != 0 && iMediaType != 0 && iMediaID != null)
            {
                t_history vNewRecord = new t_history();
                vNewRecord.AudienceID = iAudienceID;
                vNewRecord.MediaType = iMediaType;
                vNewRecord.MediaID = iMediaID;
                vNewRecord.PlaybackDate = DateTime.Now;

                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();
                    dbConnection.t_history.InsertOnSubmit(vNewRecord);
                    dbConnection.SubmitChanges();

                    return true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Fehler beim Speichern des Wiedergabe-History-Eintrags: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return false;
        }

        public static List<Objects.PlaybackHistoryRecord> getAllRecords(int iMediaType, int iMediaID)
        {
            List<Objects.PlaybackHistoryRecord> lRecords = new List<PlaybackHistoryRecord>();

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                var vRecords = from records in dbConnection.t_history where records.MediaType == iMediaType && records.MediaID == iMediaID select records.ID;

                if (vRecords != null && vRecords.Count() > 0)
                {
                    foreach (int iRecordID in vRecords)
                    {
                        if (iRecordID != 0)
                        {
                            Objects.PlaybackHistoryRecord oRecord = new PlaybackHistoryRecord(iRecordID);
                            if (oRecord != null && oRecord.RecordID != 0)
                            {
                                lRecords.Add(oRecord);
                            }
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Abrufen aller Wiedergabe-History-Einträge für " + Tools.getMediaTypeName(iMediaType) + "-ID '" + iMediaID.ToString() + "': " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return lRecords;
        }

        public static Objects.PlaybackHistoryRecord getLastPlaybackRecords(Objects.Audition oAudition)
        {
            Objects.PlaybackHistoryRecord oRecord = null;

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();                

                var vRecords = from records in dbConnection.t_history where records.AudienceID == oAudition.AuditionID select records.ID;

                if (vRecords.Count() > 0)
                {
                    var vRecord = (from records in dbConnection.t_history where records.AudienceID == oAudition.AuditionID select records.ID).Max();

                    oRecord = new PlaybackHistoryRecord((int)vRecord);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Abrufen des letzten Wiedergabe-History-Eintrags für \"" + oAudition.Name + "\": " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return oRecord;
        }


    }
}
